<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Request;
use think\Loader;
use app\admin\model\Cate as CateModel;

class Cate extends Common
{

    public function edit(){
        $cate = new CateModel();
        //获取表单数据并修改
        if(request()->isPost()){
            $data = input('post.');
            $validate = Loader::validate('Cate');
            if (!$validate->scene('edit')->check($data)) {
                $this->error($validate->getError());
            }
            $save = $cate->save(input('post.'),['id'=>input('id')]);
            if ($save !== false) {
                $this->success('修改栏目成功','lst');
            }else{
                $this->error('栏目修改失败');
            }
        }
        //显示要修改的name值
        $cates = $cate->find(input('id'));

        //显示上级栏目
        // $cateres = $cate->catetree();
        $this->assign(array(
            // 'cateres'=>$cateres,
            'cates'=>$cates,
        ));
        return view();
    }

    public function add(){
        $cate = new CateModel();
        if (request()->isPost()) {
            $data = input('post.');
            $validate = Loader::validate('Cate');
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }
            $add = $cate->addcate($data,1);
            if ($add) {
                $this->success('添加栏目成功','lst');
            }else {
                $this->error('添加栏目失败');
            }
        }
        return view();
    }

    public function lst(){
        $cate = new CateModel();
//        return dump(1);
        $cateres = $cate->getcate(1);
        $this->assign('cateres',$cateres);
        return view();
    }

    public function del(){
        $del = db('cate')->delete(input('id'));
        if ($del) {
            $this->success('删除栏目成功','lst');
        }else {
            $this->error('删除栏目失败');
        }
    }

}
